﻿
Imports System.Text
Imports osi

Public Module cmdl
    Private ReadOnly agent As input.console_key_board_agent = Nothing

    Sub New()
        timelimitOperation.disable()
        envs.disable_perftrace()
        envs.disable_nettrace()
        logic.initial()
        fs.map_accessor.register()
        resolver.global.fix()
        assert(output.output.register(output.constants.mode.text_based, New output.console_text_based_deliverer()))
        output.output.fix()
        agent = New input.console_key_board_agent()
    End Sub

    Private Function action(ByVal cmd As String) As eventComb
        Return New eventComb(Function() As Boolean
                                 If strsame(cmd, "time", False) Then
                                     Return waitfor(output.output.deliver(
                                                            output.constants.mode.text_based,
                                                            output.text_based.writeline(longTime()))) AndAlso
                                            goto_end()
                                 Else
                                     Return goto_end()
                                 End If
                             End Function)
    End Function

    Public Sub main(ByVal args() As String)
        startmain()
        Dim ec As eventComb = Nothing
        Dim last As StringBuilder = Nothing
        last = New StringBuilder()
        Dim pc As pointer(Of input.case) = Nothing
        pc = New pointer(Of input.case)()
        ec = New eventComb(Function() As Boolean
                               Return waitfor(input.input.get(pc)) AndAlso
                                      goto_next()
                           End Function,
                           Function() As Boolean
                               assert(Not +pc Is Nothing)
                               If (+pc).category = input.constants.category.key_board AndAlso
                                  (+pc).action = input.constants.key_board.action.press Then
                                   assert_waitfor(output.output.deliver(
                                                          output.constants.mode.text_based,
                                                          output.text_based.case(Convert.ToChar((+pc).mask))))
                                   'enter
                                   If (+pc).mask = 13 Then
                                       assert_waitfor(output.output.deliver(
                                                              output.constants.mode.text_based,
                                                              output.text_based.case(character.newline)))
                                       Dim cmd As String = Nothing
                                       cmd = Convert.ToString(last)
                                       If strsame(cmd, "exit", False) OrElse
                                          strsame(cmd, "quit", False) Then
                                           Return goto_end()
                                       Else
                                           Dim action_ec As eventComb = Nothing
                                           action_ec = action(Convert.ToString(last))
                                           last.Length() = 0
                                           assert_waitfor(action_ec)
                                       End If
                                   Else
                                       last.Append(Convert.ToChar((+pc).mask))
                                   End If
                               End If
                               Return goto_begin()
                           End Function)
        assert_begin(ec)
        While Not ec.end()
            sleep()
        End While
        finishmain()
    End Sub
End Module
